﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.IO;
using System.Windows.Forms;
using System.Xml;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Electrical;
using YArchitech.LIB.YJKControls;
using HYElectricCommon;

namespace HYElectricSystemSetting
{
	public partial class ConduitAndFamily : System.Windows.Forms.Form
	{
		public ConduitAndFamily(Document doc)
		{
			this.InitializeComponent();
			this.m_docment = doc;
		}

		private void ConduitAndFamily_Load(object sender, EventArgs e)
		{
		}

		private IList<Element> GetAllConduitType()
		{
			return new FilteredElementCollector(this.m_docment).OfClass(typeof(ConduitType)).ToElements();
		}

		private string GetConduitFamily(string selectItems)
		{
			try
			{
				IList<Element> allConduitType = this.GetAllConduitType();
				if (allConduitType.Count <= 0)
				{
					MessageBox.Show("检测到项目中没有所需要的线管族，请先加载族。", "盈建科提示");
					return "";
				}
				foreach (Element element in allConduitType)
				{
					Parameter parameter = element.get_Parameter(BuiltInParameter.ALL_MODEL_FAMILY_NAME);
					if (!this.cbxConduitFamily.Items.Contains(parameter.AsString()))
					{
						this.cbxConduitFamily.Items.Add(parameter.AsString());
					}
				}
				this.Comboxselct(this.cbxConduitFamily, selectItems);
			}
			catch (Exception)
			{
			}
			return this.cbxConduitFamily.SelectedItem.ToString();
		}

		private string GetFitting(int value, ComboBox cbx, string selectItems)
		{
			try
			{
				IList<Element> allConduitFitting = this.GetAllConduitFitting();
				if (allConduitFitting.Count <= 0)
				{
					MessageBox.Show("检测到项目没有所需要的配件族，请先加载族。", "盈建科提示");
					return "";
				}
				foreach (Element element in allConduitFitting)
				{
					if ((element as FamilySymbol).Family.get_Parameter(BuiltInParameter.FAMILY_CONTENT_PART_TYPE).AsInteger() == value)
					{
						this.defaultValue.Add(element.Id);
						Parameter parameter = element.get_Parameter(BuiltInParameter.ALL_MODEL_FAMILY_NAME);
						if (!cbx.Items.Contains(parameter.AsString()))
						{
							cbx.Items.Add(parameter.AsString());
						}
					}
				}
				this.Comboxselct(cbx, selectItems);
			}
			catch (Exception)
			{
			}
			return cbx.SelectedItem.ToString();
		}

		public void Comboxselct(ComboBox cb, string selectItem)
		{
			for (int i = 0; i < cb.Items.Count; i++)
			{
				if (cb.Items[i].ToString() == selectItem)
				{
					cb.SelectedIndex = i;
					return;
				}
			}
			if (cb.Items.Count > 0)
			{
				cb.SelectedIndex = 0;
				return;
			}
			MessageBox.Show("检测到项目中没有所需要的族，请先加载族。", "盈建科提示");
		}

		private string GetFittingType(ComboBox cbx, string cbxtext, string selectItems)
		{
			try
			{
				cbx.Items.Clear();
				IList<Element> allConduitFitting = this.GetAllConduitFitting();
				if (allConduitFitting.Count <= 0)
				{
					MessageBox.Show("当前项目中没有匹配的配件类型。", "盈建科提示");
					return "";
				}
				foreach (Element element in allConduitFitting)
				{
					if (element.get_Parameter(BuiltInParameter.ALL_MODEL_FAMILY_NAME).AsString() == cbxtext)
					{
						cbx.Items.Add(element);
					}
				}
				this.Comboxselct(cbx, selectItems);
			}
			catch (Exception)
			{
			}
			return cbx.SelectedIndex.ToString();
		}

		private IList<Element> GetAllConduitFitting()
		{
			FilteredElementCollector filteredElementCollector = new FilteredElementCollector(this.m_docment);
			filteredElementCollector.OfClass(typeof(FamilySymbol)).OfCategory(BuiltInCategory.OST_ConduitFitting);
			return filteredElementCollector.ToElements();
		}

		public List<ElementId> DefaultValue
		{
			get
			{
				return this.defaultValue;
			}
			set
			{
				this.defaultValue = value;
			}
		}

		private void cbxConduitFamily_SelectedIndexChanged(object sender, EventArgs e)
		{
			try
			{
				this.cbxConduitType.Items.Clear();
				IList<Element> allConduitType = this.GetAllConduitType();
				if (allConduitType.Count <= 0)
				{
					MessageBox.Show("当前项目中没有匹配的配件类型", "盈建科提示");
				}
				else
				{
					foreach (Element element in allConduitType)
					{
						if (element.get_Parameter(BuiltInParameter.ALL_MODEL_FAMILY_NAME).AsString() == this.cbxConduitFamily.Text)
						{
							this.cbxConduitType.Items.Add(element.Name);
						}
					}
					this.Comboxselct(this.cbxConduitType, "");
				}
			}
			catch (Exception)
			{
			}
		}

		private void cmbConduitType_SelectedIndexChanged(object sender, EventArgs e)
		{
		}

		private void cbxConduitType_SelectedIndexChanged(object sender, EventArgs e)
		{
			this.SaveDlgData();
		}

		private void cmbBowl_SelectedIndexChanged(object sender, EventArgs e)
		{
			this.SaveDlgData();
		}

		private void cmbthree_SelectedIndexChanged(object sender, EventArgs e)
		{
			this.SaveDlgData();
		}

		private void cmbfour_SelectedIndexChanged(object sender, EventArgs e)
		{
			this.SaveDlgData();
		}

		private void cmbTransition_SelectedIndexChanged(object sender, EventArgs e)
		{
			this.SaveDlgData();
		}

		public bool SetListValue()
		{
			string text = AssistFunc.GetAppRootPath() + "\\UserWork\\" + "ConduitAndFamily.xml";
			XmlDocument xmlDocument = new XmlDocument();
			if (!new FileInfo(text).Exists)
			{
				MessageBox.Show("缺少" + text + "文件", "盈建科提示");
				return false;
			}
			try
			{
				xmlDocument.Load(text);
				xmlDocument.SelectSingleNode("ConduitAndFamily");
			}
			catch (Exception)
			{
				return false;
			}
			return true;
		}

		public bool SaveDataToXml()
		{
			return true;
		}

		private void SaveDlgData()
		{
		}

		private void ConduitAndFamily_FormClosing(object sender, FormClosingEventArgs e)
		{
			this.SaveDataToXml();
		}

		private void cmbConduitType_MouseClick(object sender, MouseEventArgs e)
		{
		}

		private Document m_docment;

		private bool ischeck;

		private bool isLoad = true;

		private List<ElementId> defaultValue = new List<ElementId>();
	}
}
